<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons'); // echarts theme
import resize from './mixins/resize'

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '314px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      let day = new Date().getDay()
      let weeks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
      let xAxisWeek = []
      xAxisWeek.push(...weeks.slice(day))
      xAxisWeek.push(...weeks.slice(0, day))

      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}',
          axisPointer: {
            type: 'shadow',
            label: {
              backgroundColor: '#6a7985'
            }
          },
          textStyle: {
            color: '#fff',
            fontStyle: 'normal',
            fontFamily: '微软雅黑',
            fontSize: 12
          }
        },
        grid: {
          top: 35,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: ['在线', '离线'],
          textStyle: {
            color: '#fff'
          }
        },
        xAxis: [
          {
            type: 'category',
            data: xAxisWeek,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#fff'],
                opacity: 0.1
              }
            }
          }
        ],
        series: [
          {
            name: '在线',
            type: 'bar',
            barGap: '0',
            barWidth: 10,
            data: [87, 23, 78, 67, 17, 57, 34],
            animationDuration,
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0],
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 1,
                      color: 'rgba(127, 128, 225, 0.7)'
                    },
                    {
                      offset: 0.9,
                      color: 'rgba(72, 73, 181, 0.7)'
                    },
                    {
                      offset: 0.31,
                      color: 'rgba(0, 208, 208, 0.7)'
                    },
                    {
                      offset: 0.15,
                      color: 'rgba(0, 208, 208, 0.7)'
                    },
                    {
                      offset: 0,
                      color: 'rgba(104, 253, 255, 0.7)'
                    }
                  ],
                  false
                )
              }
            }
          },
          {
            name: '离线',
            type: 'bar',
            barGap: '0',
            // stack: "vistors",
            barWidth: 10,
            data: [80, 52, 44, 68, 56, 77, 81],
            animationDuration,
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0],
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 1,
                      color: 'rgba(127, 128, 225, 0.7)'
                    },
                    {
                      offset: 0.9,
                      color: 'rgba(72, 73, 181, 0.7)'
                    },
                    {
                      offset: 0.25,
                      color: 'rgba(226, 99, 74, 0.7)'
                    },
                    {
                      offset: 0,
                      color: 'rgba(253, 200, 106, 0.7)'
                    }
                  ],
                  false
                )
              }
            }
          }
        ]
      })
    }
  }
}
</script>
